//
//  ProfessionDossier.m
//  jinnengExam
//
//  Created by mac on 2020/9/14.
//  Copyright © 2020 jznrj. All rights reserved.
//

#import "ProfessionDossier.h"
#import "ClassMateViewController.h"
#import "CourseDossier.h"
@interface ProfessionDossier ()

@end

@implementation ProfessionDossier

- (void)viewDidLoad {
    [super viewDidLoad];
    UINavigationBar * bar = self.navigationController.navigationBar;
     UIImage *bgImage = [ColorUtil navigationBarColor:[ColorUtil titleColor] BarSize:bar.frame alphe:1.0];
     [bar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
     [bar setTintColor:[UIColor whiteColor]];
     [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]}forState:UIControlStateNormal];
     [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
    
    self.tabBarController.tabBar.frame = CGRectMake(0, 0, 0, 0);
    self.tabBarController.tabBar.hidden = YES;
    
    self.title = @"培训详情";
    [self initView];
    // Do any additional setup after loading the view.
}

-(void) viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void) initView{
     [self.view addSubview:self.jznWebView];
     [self.jznWebView mas_makeConstraints:^(MASConstraintMaker *make) {
          make.left.right.equalTo(self.view);
         make.top.equalTo(self.view);
          if (@available(iOS 11.0, *)) {

              make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
          } else {
              make.bottom.equalTo(self.view);
          }
      }];
    self.jznWebView.scrollView.bounces = NO;
     self.view.backgroundColor = [UIColor whiteColor];
     [self.jznWebView loadLocalFile:@"/ios_jinneng/professionDossier" type:@"html"];
    [self registerJSFuntion];
}

-(void) registerJSFuntion{
    [self.webViewBridge registerHandler:@"toClassMate" handler:^(id data, WVJBResponseCallback responseCallback) {
       NSString *classId = data[@"professionalClassId"];
       dispatch_async(dispatch_get_main_queue(), ^(){
             ClassMateViewController *vc = [[ClassMateViewController alloc]init];
             vc.professionalClassId = classId;
             [self.navigationController pushViewController:vc animated:YES];
       });
   }];
    
    [self.webViewBridge registerHandler:@"toCourse" handler:^(id data, WVJBResponseCallback responseCallback) {
       int classId = [data[@"professionalClassId"] intValue];
       dispatch_async(dispatch_get_main_queue(), ^(){
             CourseDossier *vc = [[CourseDossier alloc]init];
             vc.trainingClassId = classId;
             [self.navigationController pushViewController:vc animated:YES];
       });
    }];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
      [self requestData];
}

-(void) requestData{
    NSString *url = [NSString stringWithFormat:@"%@/professionalClass/findClassById" ,[URLUtil hostName]];
    NSDictionary *params = @{@"professionalClassId":[NSString stringWithFormat:@"%d",self.trainingClassId]};
    
    [JZNRequestUtil requestUrl:url requestParams:params success:^(DataResult *dataResult) {
        if(dataResult.code == 0){

            
            NSString *clear = [NSString stringWithFormat:@"getTrainingClass(%@)",[dataResult.data mj_JSONString]];
            [self.jznWebView evaluateJavaScript:clear completionHandler:^(id _Nullable data, NSError * _Nullable error) {
                
            }];

        }else {
        }
    } failure:^(NSError *error) {
        
    }];
}
@end
